package com.carleasoft.mps.adu.business.dict.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.carleasoft.mps.adu.business.dict.bean.entity.SysDict;
import com.carleasoft.mps.adu.business.dict.bean.po.SysDictPo;
import com.carleasoft.mps.adu.business.dict.dao.SysDictMapper;
import com.carleasoft.mps.adu.business.dict.service.SysDictService;
import com.carleasoft.mps.adu.common.constants.BusinessErrorEnum;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.data.redis.IRedisDataSource;
import com.carleasoft.mps.data.redis.JedisService;
import com.carleasoft.mps.data.redis.base.CommonCacheEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @author yanghao
 * @date 2021-12-17 17:26
 * @project mps
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {


    @Autowired
    private JedisService jedisService;
    @Autowired
    private IRedisDataSource redisDataSource;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(SysDict sysDict) {
        SysDict sysDictInfo = this.getOne(Wrappers.<SysDict>lambdaQuery()
                .eq(SysDict::getType, sysDict.getType())
                .eq(SysDict::getDictValue, sysDict.getDictValue()), false);
        if (null != sysDictInfo) {
            // 数据已经存在
            throw new BusinessException(BusinessErrorEnum.SOURCE_EXIST);
        }
        sysDict.setId(IdWorker.getIdStr());
        sysDict.setCode(IdWorker.getIdStr());
        jedisService.hmset(CommonCacheEnum.SYS_DICT.getKey(),sysDict,sysDict.getId().toString());
        boolean isOk = this.save(sysDict);
        return isOk;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDict(SysDict sysDict) {

        SysDict dict = this.getById(sysDict.getId());
        if (dict == null) {
            throw new BusinessException(BusinessErrorEnum.SOURCE_EXIST);
        }
        boolean isOk = this.updateById(sysDict);
        return isOk;
    }

    @Override
    public List<SysDict> doFindList(SysDictPo sysDict) {

        LambdaQueryWrapper<SysDict> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(sysDict.getRemark())) {
           wrapper.eq(SysDict::getRemark, sysDict.getRemark());
        }
        if (StrUtil.isNotBlank(sysDict.getType())){
            wrapper.eq(SysDict::getType, sysDict.getType());
        }
        return this.list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDict(String id) {
        SysDict dict = this.getById(id);
        if (dict == null) {
            throw new BusinessException(BusinessErrorEnum.SOURCE_EXIST);
        }
        return this.removeById(id);
    }

    @Override
    public List<SysDict> pageQuery( SysDictPo sysDictPo) {
        return this.getBaseMapper().doPageQuery(sysDictPo);
    }

    /**
     * 数据查询通过redis,当数据缺失的情况下走当前方法
     * @param ids 字典id
     * @return  List<SysDict>
     */
    @Override
    public List<SysDict> getByDictCodes(List<String> ids) {
        List<SysDict> sysDicts = this.listByIds(ids);
        Jedis jedis = redisDataSource.getRedisClient();
        if (CollectionUtil.isEmpty(sysDicts)){
            for (String id : ids) {
                jedis.hset(CommonCacheEnum.SYS_DICT.getKey(), id, "{}");
            }
        }else {
            for (SysDict sysDict : sysDicts) {
                jedis.hset(CommonCacheEnum.SYS_DICT.getKey(),sysDict.getId().toString(), JSONUtil.toJsonStr(sysDict));
            }
        }
        return sysDicts;
    }


}
